<?php

class CONFIGURACOES{
  private $servidor="localhost";
  private $usuarioBD="lucas";
  private $senhaBD="7373621346";
  private $BD="continuum";

  public $db_con;

  public function __construct(){
    if(!isset($this->db)){
      try{
        $pdo=new PDO("mysql:host=".$this->servidor.";dbname=".$this->BD, $this->usuarioBD, $this->senhaBD);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->db_con=$pdo;
      }catch(PDOEXCEPTION $e){
        die("Erro ao conectar-se ao banco de dados: ".$e->getMessage());
      
      }
    }
  }

  public function logarUsuario($email, $senha)
  {
    $stmt=$this->db_con->prepare("SELECT * FROM usuarios WHERE email=:email");
    $stmt->execute(array(":email"=>$email));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    if($row['senha']==$senha){
      return $count;
    }else{
      return false;
    }
  }
  
  public function obterDados($uid, $dado)
  {
    $stmt=$this->db_con->prepare("SELECT * FROM usuarios WHERE uid=:uid");
    $stmt->execute(array(":uid"=>$uid));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    
    $email = $row['email'];
    $senha = $row['senha'];
    $nome = $row['nome'];
    $IP = $row['IP'];
    $tempo_registro = $row['tempo_registro'];
    
    if($dado=="email"){
      echo $email;
    }
    
    if($dado=="IP"){
      echo $IP;
    }
    
    if($dado=="senha"){
      echo $senha;
    }
    
    if($dado=="tempo_registro"){
      echo $tempo_registro;
    }
    
    if($dado=="nome"){
      echo $nome;
    }
    
  }
  
    public function obterUIDporEmail($email)
  {
    $stmt=$this->db_con->prepare("SELECT * FROM usuarios WHERE email=:email");
    $stmt->execute(array(":email"=>$email));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    
    $uid = $row['uid'];
      echo $uid;
    }
  
  public function alterarDados($novo, $uid, $dado)
  { 
    if($dado=="nome"){
    $stmt=$this->db_con->prepare("UPDATE usuarios SET nome=:novo WHERE uid=:uid");
    $stmt->execute(array(":uid"=>$uid, ":novo"=>$novo));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    }
    if($dado=="email"){
    $stmt=$this->db_con->prepare("UPDATE usuarios SET email=:novo WHERE uid=:uid");
    $stmt->execute(array(":uid"=>$uid, ":novo"=>$novo));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    }
    if($dado=="senha"){
    $stmt=$this->db_con->prepare("UPDATE usuarios SET senha=:novo WHERE uid=:uid");
    $stmt->execute(array(":uid"=>$uid, ":novo"=>$novo));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    }
    
  }
  
  public function apagarConta($uid)
  {
    $stmt=$this->db_con->prepare("DELETE FROM usuarios WHERE uid=:uid");
    $stmt->execute(array(":uid"=>$uid));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $count=$stmt->rowCount();
    }
  

  public function registrarUsuario($nome, $senha, $email, $tempo_registro, $IP)
  {
    $sql="INSERT into usuarios(nome, senha, email, tempo_registro, IP) VALUES (:nome, :senha, :email, :tempo_registro, :IP)";
    $stmt=$this->db_con->prepare($sql);
    $stmt->bindParam(":nome", $nome);
    $stmt->bindParam(":senha", $senha);
    $stmt->bindParam(":email", $email);
    $stmt->bindParam(":tempo_registro", $tempo_registro);
    $stmt->bindParam(":IP", $IP);
    $stmt->execute();
    $UID=$this->db_con->lastInsertId();
    return $UID;
  }
}
